package com.copm.ifm.servers.sys.controller;

import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.client.SysRoleClient;
import com.copm.ifm.servers.sys.dto.SysRoleDTO;
import com.copm.ifm.servers.sys.entity.SysRole;
import com.copm.ifm.servers.sys.service.SysRoleService;
import com.copm.ifm.servers.sys.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统角色表相关接口类
 *
 * @author tp
 * @date 2020/12/15 11:39
 */
@Api(tags = "系统角色/sys-role")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController extends BaseController implements SysRoleClient {

    @Resource
    SysRoleService sysRoleService;

    @Override
    @ApiOperation("条件查询-分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysRoleVO>> page(@RequestBody PageDTO<SysRoleDTO> param) {
        PageVO<SysRoleVO> result = sysRoleService.selectForPage(param);
        return success(result);
    }

    @Override
    @ApiOperation("条件查询-不分页")
    @PostMapping("/list")
    public ServiceResponse<List<SysRoleVO>> list(@RequestBody SysRoleDTO queryDTO) {
        return success(sysRoleService.list(queryDTO));
    }

    @Override
    @ApiOperation("查询-根据by id")
    @GetMapping("/by/id")
    public ServiceResponse<SysRoleVO> getById(@ApiParam(value = "主键id", required = true)
                                              @RequestParam int id) {
        return success(sysRoleService.selectById(id));
    }

    @Override
    @ApiOperation("保存")
    @PostMapping
    public ServiceResponse<SysRole> save(@RequestBody SysRole entity) {
        sysRoleService.saveSysRole(entity);
        return success(entity);
    }

    @ApiOperation("修改-update")
    @PutMapping
    public ServiceResponse<SysRole> update(@RequestBody SysRole entity) {
        sysRoleService.updateEntityById(entity);
        return success(entity);
    }

    @ApiOperation("删除-byId")
    @DeleteMapping("/id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true)
                                         @RequestParam int id) {
        sysRoleService.updateDeleted(id);
        return success();
    }

    @PostMapping("/auth/multi")
    public ServiceResponse<?> auth(@RequestParam Integer roleId,
                                   @RequestBody List<Integer> userIdList) {
        sysRoleService.multiAuth(roleId, userIdList);
        return success();
    }

    @ApiOperation("系统用户查询by id")
    @GetMapping("/sys/by/id")
    public ServiceResponse<SysRoleVO> getSysById(@ApiParam(value = "主键id", required = true)
                                                 @RequestParam int id) {
        return success(sysRoleService.selectSysById(id));
    }

    @ApiOperation("系统用户查询by tenantId")
    @GetMapping("/sys/by/tenantId")
    public ServiceResponse<List<SysRoleVO>> getSysByTenantId(@ApiParam(value = "主键id", required = true)
                                                 @RequestParam int tenantId) {
        return success(sysRoleService.selectSysByTenantId(tenantId));
    }

}
